<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>时令字典</title>
  <!--引入vue.js库-->
  <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> -->
  <script src="/js/vue.js"></script>
  <!-- element-ui引入样式 展示数据的-->
  <link rel="stylesheet" href="/css/index.css">
  <!-- element-ui引入组件库 -->
  <script src="/js/index.js"></script>
  <!--引入axios库  从后端获取数据-->
  <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script>-->
  <script src="/js/axios.min.js"></script>
</head>
<body >
<!--vue必须绑定根标签-->
<div id="app">
  <!--搜索-->
  <el-row>
    <!--一行分为4列-->
    <el-col :span="3">
      <el-input v-model="searchForm.name" size="mini" placeholder="请输入部门名称"></el-input>

    </el-col>
    <el-col :span="3">
      <el-input v-model="searchForm"  size="mini" placeholder="未启用！！"></el-input>
    </el-col>
    <el-col :span="2">
      &nbsp;&nbsp; <el-button type="primary" icon="el-icon-search" size="mini" @click="search">搜索</el-button>&nbsp;
    </el-col>
    <el-col :span="2">

      <el-button type="primary" icon="el-icon-circle-plus-outline" size="mini" @click="handleAdd">添加</el-button>
    </el-col>
    <el-col :span="14">&nbsp;</el-col>
  </el-row>
  <!--table 数据-->
  <el-table
          :data="tableData"
          style="width: 100%">
    <el-table-column
            prop="code"
            label="编号"
            width="180">
    </el-table-column>
    <el-table-column
            prop="name"
            label="姓名"
            width="180">
    </el-table-column>
    <el-table-column
            prop="isedit"
            :formatter="getIsedit"
            label="是否允许修改"
            width="180" >
    </el-table-column>
    <el-table-column
            prop="isvalid"
            :formatter="getIsvalid"
            label="是否有效"
            width="180">
    </el-table-column>
    <!-- <el-table-column
             prop="orderNumber"
             label="排序号"
             width="180">
     </el-table-column>-->
    <el-table-column
            prop="description"
            label="描述"
            width="180">
    </el-table-column>
    <el-table-column label="操作" fixed="right">
      <template slot-scope="scope">
        <el-button
                size="mini"
                type="primary"
                @click="handleEdit(scope.$index, scope.row)">更新</el-button>
        <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!--分页功能-->
  <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2,5,10,15,20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
  </el-pagination>

  <!--添加或者更新的form表单-->
  <el-dialog :title="title" :visible.sync="dialogFormVisible">
    <el-form :model="form">
      <el-form-item label="姓名" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="是否允许修改" :label-width="formLabelWidth">
        <el-select v-model="form.isedit" placeholder="是否允许修改">
          <el-option label="允许修改" value="0"></el-option>
          <el-option label="不允许修改" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" :label-width="formLabelWidth">
        <el-select v-model="form.isvalid" placeholder="是否有效">
          <el-option label="有效" value="1"></el-option>
          <el-option label="无效" value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="addOrUpdate">确 定</el-button>
    </div>
  </el-dialog>
  <el-dialog
          title="提示"
          :visible.sync="dialogVisible"
          width="30%"
  >
    <span>确认删除</span>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="DeleteB">确 定</el-button>
  </span>
  </el-dialog>
</div>
<script>
  //js代码
  var vue = new Vue({
    el:"#app",
    data(){ //存放使用的所有属性数据
      return{ //方便编写表单校验
        tableData: [],//表格数据
        currentPage:1,//页码 第几页
        pageSize:5,//每页显示数量
        total:100,//分页总数量
        searchForm:{name:''},//封装查询对象
        dialogFormVisible: false,//是否显示对话框
        title:'部门添加',//默认显示title
        form:{name:''}, //form表单
        formLabelWidth: '120px',//labal宽度
        del:[],
        dialogVisible:false,
      }
    },
    created:function(){//页面初始化时，需要执行的业务可以放入该方法
      //调用加载数据方法
      this. loadData();
    },
    methods:{ //存放所有使用的方法（函数）
      //封装请求数据的方法
      loadData(){
        //定义变量_this=tshi
        var _this = this;
        //axios向后台发请求
        axios.post('/season/queryPage', //请求的url
                {"pageNo":this.currentPage,"pageSize":this.pageSize,
                  "data":{"name":this.searchForm.name}})//请求参数  json格式
                .then(function (response) {//回调函数
                  //前端控制台打印  response 是json对象
                  console.log(response);
                  console.log(JSON.stringify(response));
                  //response获取后台数据时，一定加data  response.data相当于从响应的body中获取
                  if(response.data.code==200){
                    //这里面直接写this相当于axios对象 不是vue
                    // this.tableData = response.data.data.list; //错误的
                    _this.tableData = response.data.data.list;
                    _this.total = response.data.data.total;
                  }
                })
                .catch(function (error) {//异常处理
                  console.log(error);
                });
      },
      //每页数量发生变化
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        //调用加载数据方法
        this. loadData();
      },
      //页码（第几页）发生变化
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage=val;
        //调用加载数据方法
        this. loadData();
      },
      //把数字转换成字符串
      getIsedit(row, column, cellValue, index) {
        //cellValue status的值
        var returnValue = '';
        if (cellValue == 0) {
          returnValue = "允许修改";
        } else {
          returnValue = "不允许修改";
        }
        return returnValue;
        // return cellValue==1?"启动":"禁用";
      },
      //把数字转换成字符串
      getIsvalid(row, column, cellValue, index) {
        //cellValue status的值
        var returnValue = '';
        if (cellValue == 1) {
          returnValue = "有效";
        } else {
          returnValue = "无效";
        }
        return returnValue;
        // return cellValue==1?"启动":"禁用";
      },
      //搜索方法
      search(){
        this.loadData();
      },
      //跳转更新
      handleEdit(index, row) {
        console.log(index, row);
        //alert(index+","+JSON.stringify(row));
        //row = {"deptName":'xxx',"loc":'xxx',deptNo:xxx}
        //让对话框显示
        this.dialogFormVisible =true;
        //让title值也发生变化
        this.title='部门更新';
        //给表单赋值
        this.form = row;
      },
      //跳转添加
      handleAdd(){
        //让对话框显示
        this.dialogFormVisible =true;
        //让title值也发生变化
        this.title='部门添加';
        //让form清空
        this.form={};
      },
      //真正添加或者更新
      addOrUpdate(){
        //定义请求路径，默认是添加
        var uri = "/season/add";
        //判断是否是更新
        if(this.title=='部门更新'){
          uri="/season/update";
        }
        //定义变量_this=tshi
        var _this = this;
        //axios向后台发请求
        axios.post(uri, //请求的url
                this.form)//请求参数  json格式
                .then(function (response) {//回调函数
                  //前端控制台打印  response 是json对象
                  console.log(response);
                  console.log(JSON.stringify(response));
                  //response获取后台数据时，一定加data  response.data相当于从响应的body中获取
                  if(response.data.code==200){
                    //提示成功
                    _this.$message({
                      message: '恭喜你，这是一条成功消息',
                      type: 'success'
                    });
                    //关闭弹框
                    _this.dialogFormVisible = false;
                    //重新加载数据
                    _this.loadData();
                  }else {
                    _this.$message.error('错了哦，这是一条错误消息');
                  }
                })
                .catch(function (error) {//异常处理
                  console.log(error);
                });
      },
      //处理删除
      handleDelete(index, row) {
        // console.log(index, row);
        this.dialogVisible=true;
        this.del = row.code;
        // console.log(row.deptNo)
        //   console.log(row.deptName)
        //  alert(index+","+JSON.stringify(row));
      },




      DeleteB(){
        let _this = this;
        axios.get("/season/deleteById?code=" + this.del)
                .then(function (response){
                  if(response.data.code==200){
                    //提示成功
                    _this.$message({
                      message: '恭喜你，这是一条成功消息',
                      type: 'success'
                    });
                    //关闭弹框
                    _this.dialogVisible = false;
                    //重新加载数据
                    _this.loadData();
                  }else {
                    _this.$message.error('错了哦，这是一条错误消息');
                  }
                })
                .catch(function (error) {//异常处理
                  console.log(error);
                });
      }
    }
  })
</script>
</body>
</html>